package com.million.commonutils;



import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: studyboy
 * @Date: 2022/3/19  11:46
 */
@Data
public class R {

    //成功
    @ApiModelProperty(value = "成功")
    private Boolean success;

    //状态码
    @ApiModelProperty(value = "状态码")
    private Integer code;

    //信息
    @ApiModelProperty(value = "信息")
    private String message;


    private Map<String, Object> data = new HashMap<String, Object>();

    //为了让别人不能自己new，只能调用自己我写的
    private R () {
    }

    //成功
    public static R ok () {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }

    //失败
    public static R error () {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("失败");
        return r;
    }

    //将字段名 已R对象返回的目的是为了链式编程，也就是R.ok.code...
    public R success (Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message (String message) {
        this.setMessage(message);
        return this;
    }

    public R code (Integer code) {
        this.setCode(code);
        return this;
    }

    public R data (String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data (Map<String, Object> map) {
        this.setData(map);
        return this;
    }


}
